package com.ruicar.afs.cloud.common.mq.rabbit.component;

import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.mq.rabbit.helper.CacheInfo;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.util.Assert;

/**
 * The type TransCodeMapComponent
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.06.20 10:43:14
 * @since 1.0
 */
public class TransCodeMapComponent implements ApplicationListener<ApplicationReadyEvent> {
    /**
     * On application event *
     *
     * @param event event
     */
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        for (String beanName : event.getApplicationContext().getBeanNamesForType(AfsMqBizProcessor.class)) {
            AfsMqBizProcessor afsMqBizProcessor = event.getApplicationContext().getBean(beanName, AfsMqBizProcessor.class);
            Assert.isTrue(!CacheInfo.CACHE_TRANS_CODE_PROCESS.containsKey(afsMqBizProcessor.getCode()), AfsEnumUtil.key(afsMqBizProcessor.getCode()) + " 交易码重复使用");
            CacheInfo.CACHE_TRANS_CODE_PROCESS.put(afsMqBizProcessor.getCode(), afsMqBizProcessor);
        }
    }
}
